package com.xteam.web.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.BeanHelper;
import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.purview.entities.SysCodeInfo;
import com.xteam.purview.service.impl.SystemCodeFace;
import com.xteam.utils.TreeNodeUtils;
import com.xteam.utils.WebTools;
import com.xteam.web.Error;

public class SysCodeController extends PurViewController implements IController {

	SystemCodeFace	face	= SystemCodeFace.getInstance();

	public SysCodeController() {
	}
	
	@Autowired
	private SystemLogService logService;

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);

		String[] ids = ReqHelper.getRequestStrings(request, "id[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		int r = face.delBatch(SysCodeInfo.class, SysCodeInfo.PROP_ID, ids);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_CODE_MGR, 
				"delBatch", ConstantLocation.LOG_DELETE_RES, "SysCodeInfo");

		Error er = r > 0 ? Error.create(r + "", "删除类别出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}
	
	/**
	 * 返回孩子链表
	 * @author tangkf  ---2012-4-15
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getChildren(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);
		String pid	= ReqHelper.getRequestString(request, "pid", TreeNodeUtils.ROOT_ID);
		PrintWriter out = response.getWriter();
		List<SysCodeInfo> list	= face.getChildrenByPid(pid);
		JSONArray jsa	= new JSONArray();
		for(SysCodeInfo sci:list){
			JSONObject jso	= JSONObject.fromObject(sci);
			jso.put("isLeaf", false);
			jso.put("expanded", false);
			jso.put("textView", sci.getCode() + "["+sci.getName() +"]");
			jsa.add(jso);
		}
		String data = jsa.toString();
		out.println(data);
		return null;
	}

	public ModelAndView find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);
		PrintWriter out = response.getWriter();
		JSONArray jsa	= TreeNodeUtils.getJSONArrayTreeNode(face.queryAll(SysCodeInfo.class), TreeNodeUtils.ROOT_ID);
		String data = jsa.toString();
		out.println(data);
		return null;
	}

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(JSON_TYPE);
		
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		String lslMethod = "save";

		SysCodeInfo fn = new SysCodeInfo();
		BeanHelper.requestToBean(fn, request);
		int r	= 0;
		
		if (CommonUtils.isEmpty(fn.getId())) {
			r	= face.save(fn);
		} else {
			r	= face.update(fn);
			lslType = ConstantLocation.LOG_MODIFY;
			lslMethod = "update";
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_CODE_MGR, 
				lslMethod, lslType, "SysCodeInfo");
		
		Error er	= r>0?Error.create(r+"", "保存编码出错！"):Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

}
